package com.foreveredu.propms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.HouseType;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.HouseTypeActionForm;

public class HouseTypeAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}


	// 跳转到收费项目列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<HouseType> HouseTypeList = managementService.findHouseTypes();
		request.setAttribute("houseTypeList", HouseTypeList);

		return mapping.findForward("houseTypeList");
	}

	// 完成收费项目添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseTypeActionForm htf = (HouseTypeActionForm) form;
		HouseType HouseType = new HouseType();
		PropertyUtils.copyProperties(HouseType, htf);
		if (htf.getName() == null || "".equals(htf.getName())) {
			request.setAttribute("houseType", HouseType);
			request.setAttribute("error", "房产类型不能为空！");
			return mapping.findForward("toHouseTypeList");
		}
		managementService.saveHouseType(HouseType);
		return mapping.findForward("toHouseTypeList");
	}

	// 跳转到收费项目修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseTypeActionForm htf = (HouseTypeActionForm) form;
		HouseType HouseType = managementService.findHouseTypeById(htf.getId());
		request.setAttribute("houseType", HouseType);

		return mapping.findForward("houseTypeUpdate");
	}

	// 保存收费项目修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HouseTypeActionForm htf = (HouseTypeActionForm) form;
		HouseType HouseType = managementService.findHouseTypeById(htf.getId());
		HouseType.setName(htf.getName());
		if(htf.getName()==null || "".equals(htf.getName()) ){
			request.setAttribute("houseType", HouseType);
			request.setAttribute("error","房产类型不能为空！");
			return mapping.findForward("houseTypeUpdate");
		}
		managementService.updateHouseType(HouseType);
		return mapping.findForward("update");
	}

	// 完成收费项目删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseTypeActionForm htf = (HouseTypeActionForm) form;
		managementService.deleteHouseTypeById(htf.getId());
		return mapping.findForward("delete");
	}

}
